package com.larry.mr.outputformat;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @ProjectName hadoop
 * @Author larry
 * @Date 14:58 2021/11/16
 * @Description TODO    自定义输出格式
 **/
public class LogDriver {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {

        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration);

        job.setJarByClass(LogDriver.class);

        job.setMapperClass(LogMapper.class);
        job.setReducerClass(LogReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        //指定自定义outputformat
        job.setOutputFormatClass(LogOutputFormat.class);

        FileInputFormat.setInputPaths(job,new Path("C:\\Users\\larry\\IdeaProjects\\hadoop\\MapReduce\\src\\main\\resources\\log\\log.txt"));
        FileOutputFormat.setOutputPath(job,new Path("C:\\Users\\larry\\IdeaProjects\\hadoop\\MapReduce\\src\\main\\resources\\log\\out1"));

        boolean flag = job.waitForCompletion(true);

        System.exit(flag ? 0 : 1);
    }
}
